package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Rural;
import com.gx.mxbbx.service.RuralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RuralController {
    @Autowired
    private RuralService ruralService;

    @RequestMapping("/getruralList")
    public String getruralList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String countyId = request.getParameter("countyIds");
        String peasantId = request.getParameter("contexts");
        String fullName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Rural> pageInfo = ruralService.getruralList(countyId, peasantId, fullName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", fullName);
        model.addAttribute("contexts", peasantId);
        return "/rural/rural_list";
    }

    @RequestMapping("/rurallook")
    public String rurallook(Integer id, Model model) {
        Rural rural = ruralService.rurallook(id);
        model.addAttribute("rural", rural);
        return "/rural/rural";
    }

    @RequestMapping("/openruralAdd")
    public String openruralAdd() {
        return "/rural/rural_add";
    }

    @RequestMapping("/ruraladd")
    public String ruraladd(Rural rural, HttpServletRequest request, Model model) {
        ruralService.ruraladd(rural);
        return getruralList(1, 10, request, model);
    }

    @RequestMapping("/ruralopenEdit")
    public String ruralopenEdit(Integer id, Model model) {
        Rural rural = ruralService.ruralopenEdit(id);
        model.addAttribute("rural", rural);
        return "/rural/rural_edit";
    }

    @RequestMapping("/ruraledit")
    public String ruraledit(Rural rural, HttpServletRequest request, Model model) {
        ruralService.ruraledit(rural);
        return getruralList(1, 10, request, model);
    }

    @RequestMapping("/ruraldelete")
    public String ruraldelete(Integer id, HttpServletRequest request, Model model) {
        ruralService.ruraldelete(id);
        return getruralList(1, 10, request, model);
    }
}
